# encoding: utf-8
from flask_marshmallow import base_fields
from flask_restplus_patched import ModelSchema
from app.modules.common.schemas import BaseSchema
from .models import ProjectConfig, ProjectDeployTime, ProjectInfo, ProjectSql, ProjectHandOverRecord, ProjectSqlRecord, \
    ProjectMergeRecord


class ProjectSvnListBaseSchema(ModelSchema):
    class Meta:
        fields = (
            'name',
            'url',
        )


class ProjectBaseSchema(ModelSchema):
    svn_list = base_fields.Nested(
        ProjectSvnListBaseSchema,
        many = True
    )

    class Meta:
        model = ProjectInfo
        fields = (
            'project_name',
            'trunk_name',
            'plan_id',
            'plan_name',
            'local_path',
            'vcs_path',
            'svn_from',
            'branch_num',
            'project_status',
            'to_test_edition',
            'testing_edition',
            'to_integration_edition',
            'integration_edition',
            'integrationing_edition',
            'hand_over_edition',
            'online_edition',
            'svn_list',
            'isOnBeetle',
            'error_desc',
            'from_version',
            'from_svn_version',
            'package_type',
            'package_name',
        )


class ProjectHandOverRecordBaseSchema(ModelSchema):
    class Meta:
        model = ProjectHandOverRecord
        fields = (
            'id',
            'project_name',
            'create_date',
            'hand_id',
            'hand_realname',
            'config_desc',
            'sql_desc',
            'other_desc',
            'type',
            'is_can_integration',
            'is_integration_test',
            'is_function_test',
            'edition_num',
            'is_cancel',
            'is_handle_bug',
        )


class ProjectConfigBaseSchema(ModelSchema):
    class Meta:
        model = ProjectConfig
        fields = (
            'integration_id',
            'integration_name',
            'trunk_name',
            'key',
            'value',
            'desc',
            'type',
            'is_ops_check',
            'file_name',
            'config_type',
            'value_from',
        )


class ProjectSqlAnalyseBaseSchema(ModelSchema):
    class Meta:
        fields = (
            'row_num',
            'row',
        )


class ProjectSqlRecordBaseSchema(ModelSchema):
    class Meta:
        model = ProjectSqlRecord
        fields = (
            'id',
            'project_name',
            'integration_id',
            'integration_name',
            'trunk_name',
            'draw_time',
            'status',
            'change_desc',
        )


class ProjectMergeRecordBaseSchema(ModelSchema):
    class Meta:
        model = ProjectMergeRecord
        fields = (
            'project_name',
            'integration_id',
            'status',
            'desc',
            'update_date',
        )


class ProjectSqlBaseSchema(ModelSchema):
    analyse = base_fields.Nested(
        ProjectSqlAnalyseBaseSchema,
        many = True
    )

    class Meta:
        model = ProjectSql
        fields = (
            'id',
            'project_name',
            'integration_id',
            'integration_name',
            'trunk_name',
            'file_name',
            'file_path',
            'file_size',
            'is_dba_check',
            'is_upload',
            'draw_time',
            'modify_date',
            'file_modify_date',
            'md5',
            'analyse',
        )


class ProjectDeployTimeBaseSchema(ModelSchema):
    class Meta:
        model = ProjectDeployTime
        fields = (
            'trunk_name',
            'zone_code',
            'deploy_start_time',
            'deploy_end_time',
            'hot_deploy',
            'has_config',
            'has_sql',
            'deployer_account',
            'deployer_realname',
            'integration_id',
        )


class ProjectListSchema(BaseSchema):
    """
    工程列表
    """
    data = base_fields.Nested(
        ProjectBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectSchema(BaseSchema):
    """
    工程列表
    """
    data = base_fields.Nested(
        ProjectBaseSchema,
        many = False
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectWithJarListBaseSchema(BaseSchema):
    """
    工程列表
    """
    trunkProject = base_fields.Nested(
        ProjectBaseSchema,
        many = True
    )
    jarProject = base_fields.Nested(
        ProjectBaseSchema,
        many = True
    )

    class Meta:
        fields = (
            'trunkProject',
            'jarProject',
        )


class ProjectWithJarListSchema(BaseSchema):
    """
    工程列表
    """
    data = base_fields.Nested(
        ProjectWithJarListBaseSchema,
        many = False
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class PlansProjectSchema(ProjectBaseSchema):
    list = base_fields.Nested(
        ProjectBaseSchema,
        many = True
    )

    class Meta:
        fields = ProjectBaseSchema.Meta.fields + (
            'list',
        )


class PlansProjectListSchema(BaseSchema):
    """
    多项目合并后工程列表
    """
    data = base_fields.Nested(
        PlansProjectSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectConfigListSchema(BaseSchema):
    data = base_fields.Nested(
        ProjectConfigBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectSqlListSchema(BaseSchema):
    data = base_fields.Nested(
        ProjectSqlBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectDeployTimeListSchema(BaseSchema):
    data = base_fields.Nested(
        ProjectDeployTimeBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectEditionListBaseSchema(ModelSchema):
    class Meta:
        fields = (
            'editionNum',
            'showNum',
        )


class ProjectEditionListSchema(BaseSchema):
    """
    工程版本列表
    """
    data = base_fields.Nested(
        ProjectEditionListBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectOnlineVersionListBaseSchema(ModelSchema):
    class Meta:
        fields = (
            'app',
            'package_path',
            'version_type',
            'editionNum',
            'projectName',
            'version',
        )


class ProjectOnlineVersionListSchema(BaseSchema):
    data = base_fields.Nested(
        ProjectOnlineVersionListBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectHandOverRecordListSchema(BaseSchema):
    data = base_fields.Nested(
        ProjectHandOverRecordBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectSqlRecordListSchema(BaseSchema):
    data = base_fields.Nested(
        ProjectSqlRecordBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )


class ProjectMergeRecordListSchema(BaseSchema):
    data = base_fields.Nested(
        ProjectMergeRecordBaseSchema,
        many = True
    )

    class Meta:
        fields = BaseSchema.Meta.fields + (
            'data',
        )
